package ml.coursedesign.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.Data;
import ml.coursedesign.common.lang.Result;
import ml.coursedesign.entity.SignVo;
import ml.coursedesign.entity.Student;
import ml.coursedesign.entity.StudentSign;
import ml.coursedesign.entity.TeacherSign;
import ml.coursedesign.service.StudentService;
import ml.coursedesign.service.StudentSignService;
import ml.coursedesign.service.TeacherSignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
public class SignController {
    @Autowired
    TeacherSignService teacherSignService;
    @Autowired
    StudentSignService studentSignService;
    @Autowired
    StudentService studentService;
    @PostMapping("/teacher/release/sign/{classid}/{teacherId}")
    public Result releaseSign(@PathVariable String classid,
                              @PathVariable String teacherId,
                              @RequestBody SignVo teacherSign){

        boolean save = teacherSignService.releaseSign(classid,teacherId,teacherSign);
        return Result.succ();
    }
    @GetMapping("/teacher/look/sign/info/{classid}/{date}")
    public Result lookSignInfo(@PathVariable String classid,
                              @PathVariable String date){

      List<StudentSign> signList = studentSignService.lookSignInfo(classid,date);

        return Result.succ(signList);
    }
    @GetMapping("/teacher/look/sign/history/{classid}/{data}")
    public Result lookSignHistory(@PathVariable String classid,
                               @PathVariable String date){

        List<TeacherSign> signList = teacherSignService.lookSignHistory(classid,date);

        return Result.succ();
    }

    @GetMapping("/student/sign/{stuid}")
    public Result studentSign(@PathVariable String stuid ){
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",stuid);
        Student student = studentService.getOne(queryWrapper);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        StudentSign studentSign = new StudentSign();
        studentSign.setStuId(stuid);
        String classId= String.valueOf(student.getClassName().charAt(student.getClassName().length()-2));
        studentSign.setClassId(classId);
        studentSign.setSignTime(sdf.format(date));
        boolean save = studentSignService.save(studentSign);
        return Result.succ();
    }

    @GetMapping("/student/signBack/{stuid}")
    public Result studentBackSign(@PathVariable String stuid){
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",stuid);
        Student student = studentService.getOne(queryWrapper);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        QueryWrapper<StudentSign> studentSignQueryWrapper = new QueryWrapper<StudentSign>();
        studentSignQueryWrapper.eq("stu_id",stuid);
        StudentSign one = studentSignService.getOne(studentSignQueryWrapper);
        one.setStuId(stuid);
        String classId= String.valueOf(student.getClassName().charAt(student.getClassName().length()-2));
        one.setClassId(classId);
        one.setSingbackTime(sdf.format(date));
        boolean save = studentSignService.saveOrUpdate(one);
        return Result.succ();
    }


    @GetMapping("/student/is/sign/{studentid}")
    public Result isSign(@PathVariable String studentid){
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",studentid);
        Student student = studentService.getOne(queryWrapper);
        String classId= String.valueOf(student.getClassName().charAt(student.getClassName().length()-2));

        Boolean isSign = teacherSignService.isSign(classId);

      return Result.succ(isSign);
    }

    @GetMapping("/student/is/signback/{studentid}")
    public Result isSignBack(@PathVariable String studentid){
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",studentid);
        Student student = studentService.getOne(queryWrapper);
        String classId= String.valueOf(student.getClassName().charAt(student.getClassName().length()-2));

        Boolean isSign = teacherSignService.isSignBack(classId);

        return Result.succ(isSign);
    }




}
